﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Twipler.Data.Cache
{
    public class TwitterUserDetails : TwitterId
    {
        public string ScreenName { get; set; }
        public string ProfileImageUrl { get; set; }

        private string _screenName = "sn";
        private int _screenNameSize = 100;
        private string _profileimageurl = "pi";
        private int _profileimageurlSize = 300;

        public override int ColumnCount
        {
            get { return base.ColumnCount + 2; }
        }

        public override List<TableColumn> Columns
        {
            get
            {
                List<TableColumn> baseList = base.Columns;
                baseList.Add(new TableColumn(false, _screenName, "varchar(" + _screenNameSize +") null", "@" + _screenName));
                baseList.Add(new TableColumn(false, _profileimageurl, "varchar(" +  _profileimageurlSize + ") null", "@" + _profileimageurl));
                return baseList;
            }
        }

    	public override string TableName
    	{
			get { return "TwitterUser";  }
    	}

    	public override  void FromSqlDataReader(SqlDataReader sqlReader)
        {
            base.FromSqlDataReader(sqlReader);
            
			ScreenName = (string)sqlReader[_screenName];
            ProfileImageUrl = (string)sqlReader[_profileimageurl];
        }

        public override void AddParameters(SqlCommand command)
        {
            base.AddParameters(command);

            command.Parameters.Add(_screenName, System.Data.SqlDbType.VarChar).Value = ScreenName;
            command.Parameters.Add(_profileimageurl, System.Data.SqlDbType.VarChar).Value = ProfileImageUrl;
        }

		public override void QueryParams(SqlCommand command, params object[] args)
		{
			base.QueryParams(command, args);

			if (args.Length >= ColumnCount - 1)
				command.Parameters.Add("@" + _screenName, SqlDbType.VarChar, _screenNameSize).Value = (string) args[ColumnCount - 2];

			if (args.Length >= ColumnCount)
				command.Parameters.Add("@" + _profileimageurl, SqlDbType.VarChar, _profileimageurlSize).Value = (string) args[ColumnCount - 1];
		}
    }
}
